﻿using System;
using BingoClientModel;
using BingoServerCommon.Db.Pool;
using BingoServerCommon.DbBusinessModel;
using BingoServerCommon.Helper;
using Dapper;
using Dapper.Contrib.Extensions;
using HPSocketCS;

namespace BingoServerCommon.Common
{
	public class Session
	{

		public IntPtr ConnId { get; set; }
		public string IpAddress { get; set; }

		public ushort Port { get; set; }
		public BasePlayer player { get; set; }


		public TcpPackServer Server = new TcpPackServer();
		/// <summary>
		/// 通用
		/// </summary>
		/// <param name="package"></param>
		public void Send(Package package)
		{
			byte[] bytes = package.GetBuffer();
			bytes = CryptologyHelper.EncryptByteArr(ref bytes);

			Server.Send(ConnId, bytes, bytes.Length);
		}

		/// <summary>
		/// 用户断线时候的保存信息
		/// </summary>
		/// <param name="ConnectionPool"></param>
		public void Save(ConnectionPool ConnectionPool)
		{
			var conn = ConnectionPool.getConnection();

			

			var result = conn.Update<player2>(new player2
			{
				nickname = "1***",
				portrait = DateTimeOffset.Now.ToLocalTime().ToString(),
				id = 1
			});

			Package pkg = new Package(EnumPackCommand.SC_ServerSave);

			if (result)
			{
				pkg.Write((byte)EnumReqRetStatus.Success);
			}
			else
			{
				pkg.Write((byte)EnumReqRetStatus.Fail0);
			}
			Send(pkg);
		}
	}
}
